package com.express.e.root.dao.impl;

import com.express.e.root.bean.Admin;
import com.express.e.root.dao.AdminDao;
import com.express.e.root.utils.DBUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AdminDaoImpl extends DBUtils implements AdminDao {
    @Override
    public Admin queryByAdmin(Admin admin) {
        Admin admin4LoginIn = new Admin();
        String sql = "select * from admin where adminName = ? and adminPwd = ? and isDeleted =0";
        ArrayList<Object> params = new ArrayList<>();
        params.add(admin.getAdminName());
        params.add(admin.getAdminPwd());
        resultSet = query(sql, params);
        if (resultSet == null) {
            return admin4LoginIn;
        }
        try {
            while (resultSet.next()) {
                admin4LoginIn.setAdminId(resultSet.getInt("adminId"));
                admin4LoginIn.setAdminName(resultSet.getString("adminName"));
                admin4LoginIn.setAdminPwd(resultSet.getString("adminPwd"));
                admin4LoginIn.setLoginIP(resultSet.getString("loginIp"));
                admin4LoginIn.setLastTime(resultSet.getDate("lastTime"));
                admin4LoginIn.setState(resultSet.getInt("state"));
                admin4LoginIn.setRole(resultSet.getInt("role"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return admin4LoginIn;
    }

    @Override
    public Boolean loginOutByAdmin(Admin admin) {
        int count = 0;
        String sql = "update admin as a set a.state = 0,a.lastTime = NOW() where a.adminName = ? and  a.adminPwd= ?;";
        ArrayList<Object> params = new ArrayList<>();
        params.add(admin.getAdminName());
        params.add(admin.getAdminPwd());
        try {
            count = update(sql, params);
        } finally {
            closeAll();
        }
        return count != 0;
    }

    @Override
    public Boolean onlineAdmin(Admin admin) {
        int count = 0;
        String sql = "update admin as a set a.state = 1,a.lastTime = NOW() where a.adminName = ? and  a.adminPwd= ?;";
        ArrayList<Object> params = new ArrayList<>();
        params.add(admin.getAdminName());
        params.add(admin.getAdminPwd());
        try {
            count = update(sql, params);
        } finally {
            closeAll();
        }
        return count != 0;
    }

    @Override
    public Boolean addAdmin(Admin admin) {
        Integer addNum = 0;
        String sql = "INSERT INTO admin (adminName,adminPwd,loginIP,lastTime,state,role,phone,cardId) VALUES(?,?,?,?,?,?,?,?);";
        ArrayList<Object> params = new ArrayList<>();
        params.add(admin.getAdminName());
        params.add(admin.getAdminPwd());
        params.add(admin.getLoginIP());
        params.add(admin.getLastTime());
        params.add(admin.getState());
        params.add(admin.getRole());
        params.add(admin.getPhone());
        params.add(admin.getCardId());
        try {
            addNum = update(sql, params);
        } finally {
            closeAll();
        }
        return addNum > 0;
    }

    @Override
    public Boolean deleteAdmin(Integer adminId) {
        Integer delNum = 0;
        String sql = "UPDATE admin AS d SET d.`isDeleted` = 1 WHERE  d.`adminId` = ?;";
        ArrayList<Object> params = new ArrayList<>();
        params.add(adminId);
        try {
            delNum = update(sql, params);
        } finally {
            closeAll();
        }
        return delNum > 0;
    }

    @Override
    public Boolean updateAdmin(Admin admin) {
        Integer updateNum = 0;
        String sql = "UPDATE admin AS a SET a.`adminName` =?,a.`adminPwd`=?,a.`phone`=?,a.`cardId`=? where a.`adminId`=?;";
        ArrayList<Object> params = new ArrayList<>();
        params.add(admin.getAdminName());
        params.add(admin.getAdminPwd());
        params.add(admin.getPhone());
        params.add(admin.getCardId());
        params.add(admin.getAdminId());
        try {
            updateNum = update(sql, params);
        } finally {
            closeAll();
        }
        return updateNum > 0;
    }

    @Override
    public List listAdmin() {
        String sql = "SELECT * FROM admin WHERE isDeleted = 0;";
        resultSet = query(sql, null);
        ArrayList<Admin> list = new ArrayList<>();
        try {
            while (resultSet.next()) {
                Admin admin = new Admin(
                        resultSet.getInt("adminId"),
                        resultSet.getString("adminName"),
                        resultSet.getString("adminPwd"),
                        resultSet.getString("loginIP"),
                        resultSet.getDate("lastTime"),
                        resultSet.getInt("state"),
                        resultSet.getInt("role"),
                        resultSet.getString("phone"),
                        resultSet.getString("cardId")
                );
                list.add(admin);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return list;
    }

    @Override
    public Admin queryById(Integer adminId) {
        Admin admin = new Admin();
        String sql = "select * from admin where adminId = ? and isDeleted =0;";
        ArrayList<Object> params = new ArrayList<>();
        params.add(adminId);
        resultSet = query(sql, params);
        try {
            while (resultSet.next()){
                admin = new Admin(
                        resultSet.getInt("adminId"),
                        resultSet.getString("adminName"),
                        resultSet.getString("adminPwd"),
                        resultSet.getString("loginIP"),
                        resultSet.getDate("lastTime"),
                        resultSet.getInt("state"),
                        resultSet.getInt("role"),
                        resultSet.getString("phone"),
                        resultSet.getString("cardId")
                );
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return admin;
    }

    @Override
    public Admin queryByName(String adminName) {
        Admin admin = new Admin();
        String sql = "select * from admin where adminName = ? and isDeleted =0;";
        ArrayList<Object> params = new ArrayList<>();
        params.add(adminName);
        try {
            resultSet = query(sql, params);
            while (resultSet.next()){
                admin = new Admin(
                        resultSet.getInt("adminId"),
                        resultSet.getString("adminName"),
                        resultSet.getString("adminPwd"),
                        resultSet.getString("loginIP"),
                        resultSet.getDate("lastTime"),
                        resultSet.getInt("state"),
                        resultSet.getInt("role"),
                        resultSet.getString("phone"),
                        resultSet.getString("cardId")
                );
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return admin;
    }

    @Override
    public Admin queryByPhone(String phone) {
        Admin admin = new Admin();
        String sql = "select * from admin where phone = ? and isDeleted =0;";
        ArrayList<Object> params = new ArrayList<>();
        params.add(phone);
        try {
            resultSet = query(sql, params);
            while (resultSet.next()){
                admin = new Admin(
                        resultSet.getInt("adminId"),
                        resultSet.getString("adminName"),
                        resultSet.getString("adminPwd"),
                        resultSet.getString("loginIP"),
                        resultSet.getDate("lastTime"),
                        resultSet.getInt("state"),
                        resultSet.getInt("role"),
                        resultSet.getString("phone"),
                        resultSet.getString("cardId")
                );
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return admin;
    }

    @Override
    public Admin queryByCardId(String cardId) {
        Admin admin = new Admin();
        String sql = "select * from admin where cardId = ? and isDeleted =0;";
        ArrayList<Object> params = new ArrayList<>();
        params.add(cardId);
        try {
            resultSet = query(sql, params);
            while (resultSet.next()){
                admin = new Admin(
                        resultSet.getInt("adminId"),
                        resultSet.getString("adminName"),
                        resultSet.getString("adminPwd"),
                        resultSet.getString("loginIP"),
                        resultSet.getDate("lastTime"),
                        resultSet.getInt("state"),
                        resultSet.getInt("role"),
                        resultSet.getString("phone"),
                        resultSet.getString("cardId")
                );
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return admin;
    }
}
